package com.micai.springboot.entity;

import java.util.Comparator;

/**
 * 描述：学生表通过Comparator实现按指定字段排序
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/5/16 16:12
 */
public class StudentEntity {

    // 学生ID
    private int id;
    // 学生名称
    private String name;
    // 学生年龄
    private int age;

    public StudentEntity() {
    }

    public StudentEntity(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 按学生名称对列表进行排序的比较器
     */
    public static Comparator<StudentEntity> nameComparator = new Comparator<StudentEntity>() {
        @Override
        public int compare(StudentEntity o1, StudentEntity o2) {
            String name1 = o1.getName().toUpperCase();
            String name2 = o2.getName().toUpperCase();
            // 升序
            return name1.compareTo(name2);
            // 降序
            //return name2.compareTo(name1);
        }
    };

    /**
     * 按学生年龄对列表进行排序的比较器
     */
    public static Comparator<StudentEntity> ageComparator = new Comparator<StudentEntity>() {
        @Override
        public int compare(StudentEntity o1, StudentEntity o2) {
            int age1 = o1.getAge();
            int age2 = o2.getAge();
	         /*升序*/
            return age1-age2;
	        /*降序*/
            //age2-age1;
        }
    };

    @Override
    public String toString() {
        return "StudentEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
